/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){
	//Global vars
	var form = $("#customForm");

	var uname = $("#uname");
	var unameInfo = $("#unameInfo");
	
	var pass1 = $("#pass1");
	var pass1Info = $("#pass1Info");
	
	var pass2 = $("#pass2");
	var pass2Info = $("#pass2Info");

	var email = $("#email");
	var emailInfo = $("#emailInfo");
		
	var firstname = $("#firstname");
	var firstnameInfo = $("#firstnameInfo");
	
	var lastname = $("#lastname");
	var lastnameInfo = $("#lastnameInfo");		
	
	var mobilephone = $("#mobilephone");
	var mobilephoneInfo = $("#mobilephoneInfo");		

	var agreement = $("#agreement");		
	var agreementInfo = $("#agreementInfo");	

	
	var message = $("#message");
	
	//On blur
	uname.blur(validateUName);
	firstname.blur(validateFirstName);	
	lastname.blur(validateLastName);	
	mobilephone.blur(validateMobilePhone);		
	email.blur(validateEmail);
	pass1.blur(validatePass1);
	pass2.blur(validatePass2);
	agreement.blur(agreement);	
	
	
	//On key press
	uname.keyup(validateUName);	
	firstname.keyup(validateFirstName);	
	lastname.keyup(validateLastName);	
	mobilephone.keyup(validateMobilePhone);	
	pass1.keyup(validatePass1);
	pass2.keyup(validatePass2);
	message.keyup(validateMessage);
	agreement.keyup(validateAgreement);	
	
	
	//On Submitting
	form.submit(function(){
		if(validateUName() && validatePass1() && validatePass2() && validateEmail() && validateFirstName() && validateLastName() && validateMobilePhone() && validateAgreement()){
			//return true;
			
      if($('ul.sortable').validate()){
        alert("Yeah, you are a human!");
        return true;                             
      }else{
        alert("No, you are not a human!"); 
        return false;
      } 			
		}else{
			return false;
		}
	});

	
	function validateUName(){
		//if it's NOT valid
		if(uname.val().length < 5){
			uname.addClass("error");
			unameInfo.text("We want user names with more than 5 letters!");
			unameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			uname.removeClass("error");
			unameInfo.text("What's your user name?");
			unameInfo.removeClass("error");
			return true;
		}
	}
	
		
	function validateFirstName(){
		//if it's NOT valid
		if(firstname.val().length < 4){
			firstname.addClass("error");
			firstnameInfo.text("We want names with more than 3 letters!");
			firstnameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			firstname.removeClass("error");
			firstnameInfo.text("What's your firstname?");
			firstnameInfo.removeClass("error");
			return true;
		}
	}
	
	function validateLastName(){
		//if it's NOT valid
		if(lastname.val().length < 4){
			lastname.addClass("error");
			lastnameInfo.text("We want names with more than 3 letters!");
			lastnameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			lastname.removeClass("error");
			lastnameInfo.text("What's your lastname?");
			lastnameInfo.removeClass("error");
			return true;
		}
	}	
		
	//validation functions
	function validateEmail(){
		//testing regular expression
		var a = $("#email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			email.removeClass("error");
			emailInfo.text("Valid E-mail please, you will need it to log in!");
			emailInfo.removeClass("error");
			return true;
		}
		//if it's NOT valid
		else{
			email.addClass("error");
			emailInfo.text("Stop cowboy! Type a valid e-mail please :P");
			emailInfo.addClass("error");
			return false;
		}
	}

	function validateMobilePhone(){
		//if it's NOT valid
		if(mobilephone.val().length < 10){
			mobilephone.addClass("error");
			mobilephoneInfo.text("We want names with more than 10 letters!");
			mobilephoneInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			mobilephone.removeClass("error");
			mobilephoneInfo.text("What's your mobile phone?");
			mobilephoneInfo.removeClass("error");
			return true;
		}
	}		
	
	
	function validatePass1(){
		var a = $("#password1");
		var b = $("#password2");

		//it's NOT valid
		if(pass1.val().length <5){
			pass1.addClass("error");
			pass1Info.text("Ey! Remember: At least 5 characters: letters, numbers and '_'");
			pass1Info.addClass("error");
			return false;
		}
		//it's valid
		else{			
			pass1.removeClass("error");
			pass1Info.text("At least 5 characters: letters, numbers and '_'");
			pass1Info.removeClass("error");
			validatePass2();
			return true;
		}
	}
	
	function validatePass2(){
		var a = $("#password1");
		var b = $("#password2");
		//are NOT valid
		if( pass1.val() != pass2.val() ){
			pass2.addClass("error");
			pass2Info.text("Passwords doesn't match!");
			pass2Info.addClass("error");
			return false;
		}
		//are valid
		else{
			pass2.removeClass("error");
			pass2Info.text("Confirm password");
			pass2Info.removeClass("error");
			return true;
		}
	}
	
	function validateMessage(){
		//it's NOT valid
		if(message.val().length < 10){
			message.addClass("error");
			return false;
		}
		//it's valid
		else{			
			message.removeClass("error");
			return true;
		}
	}
	
	
	function validateAgreement(){
		//if it's valid
		if(agreement.is(':checked')){
			agreement.removeClass("error");
			agreementInfo.removeClass("error");
			return true;			
		}
		//if it's NOT valid		
		else{
			agreement.addClass("error");
			agreementInfo.text("Please check agreement?");
			agreementInfo.addClass("error");
			return false;
		}
	}
		
});